<?php

declare(strict_types=1);

namespace app\model;

use think\facade\Db;
use think\Model;

/**
 * @mixin \think\Model
 */
class Options extends Model
{
    //系统配置
    public static function getOption($name, $isAdmin = false)
    {
        if ($isAdmin) return Db::name('options')->where('name', $name)->value('value');
        return Db::name('options')->where('name', $name)->where('protect', 0)->value('value');
    }

    public static function getOptions($type = ['base'], $isAdmin)
    {
        if ($isAdmin) return Db::name('options')->where('type', 'in', $type)->column('value');
        return Db::name('options')->where('type', 'in', $type)->where('protect', 0)->column('value');
    }

    public static function getAll($isAdmin = false)
    {
        if ($isAdmin) return Db::name('options')->select();
        return Db::name('options')->where('protect', 0)->select();
    }

    public static function setOption($name, $val)
    {
        return Db::name('options')->where('name', $name)->update(['value' => $val]);
    }

    public static function setOptions($data)
    {
        foreach ($data as $key => $value) {
            return Db::name('options')->where('name', $key)->update(['value' => $value]);
        }
    }
}
